<template>
    <div>
        <div class="user_search">
            <Button @click="createOne">新增管理</Button>
        </div>
        <Mytable :listColumns="listColumns" :listData="listData" />
    </div>
</template>
<script>
import Mytable from '@/components/Mytable.vue';
import {remove, selectAll} from "@/api/admin/admin";
import dayjs from "dayjs";
export default {
    name: 'adminList',
    components: {
        Mytable,
    },
    data() {
        return {
            listColumns: [
                { title: '管理员账号', key: 'userId' },
                { title: '手机号', key: 'tel' },
                { title: '创建时间', key: 'createTime' },
                { title: '创建人', key: 'createdBy' },
                {
                    title: '操作', key: 'operate',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'text',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                      console.log(params.row.userId)
                                        this.$router.push({name:'editAdmin',params:{id:params.row.userId}})
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'text',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.deleteAdmin(params.row.userId)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            ],
            listData: [],
        }
    },
    methods: {
        createOne() {
            this.$router.push({
                name:'createAdmin'
            })
        },
        deleteAdmin(id) {
          remove(id).then(res => {
              if(res.data.code === 200){
                  this.$Message.success('删除成功')
                // 重新获取数据
                  selectAll().then(res => {
                      this.listData = res.data.data;
                      this.listData.filter(item => {
                          item.createTime = dayjs(item.createTime).format('YYYY-MM-DD')
                      })
                  })
              }else{
                  this.$Message.error('删除失败')
              }
          })
        }
    },
  created() {
      selectAll().then(res => {
          this.listData = res.data.data;
          this.listData.filter(item => {
              item.createTime = dayjs(item.createTime).format('YYYY-MM-DD')
          })
      })
  }
}
</script>
<style scoped>
.user_search {
    margin-bottom: 10px;
}
</style>